# *********************************************************************
class Point(object):
	
	# -------------------------------------------------------------------
	def __init__(self, x = 0, y = 0):
		self.Set(x, y)
		
	# -------------------------------------------------------------------
	def Set(self, x, y):
		self.x	=	x
		self.y	=	y
		
	# -------------------------------------------------------------------
	def InRect(self, rt):
		try:
			for r in rt:
				if self.InRect(r):
					return True
			
			return False
		except:
			pass
		
		if self.x >= rt.x and self.x < rt.x + rt.width:
			if self.y >= rt.y and self.y < rt.y + rt.height:
				return True
				
		return False

# *********************************************************************
class Rect(object):
	
	# -------------------------------------------------------------------
	def __init__(self, x = 0, y = 0, width = 0, height = 0):
		self.Set(x, y, width, height)
		
	# -------------------------------------------------------------------
	def Valid(self):
		return self.width > 0 and self.height > 0
		
	# -------------------------------------------------------------------
	def Set(self, x, y, width, height):
		self.x			=	x
		self.y			=	y
		self.width	=	width
		self.height	=	height
		
	# -------------------------------------------------------------------
	def Tuple(self):
		return (self.x, self.y, self.width, self.height)
		
	# -------------------------------------------------------------------
	def Contains(self, x, y):
		if x < self.x or x > self.x + self.width:
			return False
		
		if y < self.y or y > self.y + self.height:
			return False
		
		return True
		
	# -------------------------------------------------------------------
	def Grow(self, dx, dy):
		self.x	=	self.x - dx
		self.y	=	self.y - dy
		self.width	=	self.width + dx * 2
		self.height	=	self.height + dy * 2
		
	# -------------------------------------------------------------------
	def Intersect(self, rt):
		try:
			for r in rt:
				if self.Intersect(r):
					return True
			
			return False
		except:
			pass
		
		left = self.x
		top = self.y
		right = self.x + rt.width
		bottom = self.y + rt.height
		
		rleft = rt.x
		rtop = rt.y
		rright = rt.x + rt.width
		rbottom = rt.y + rt.height
		
		checky	=	False
		
		if left >= rleft:
			if left < rright:
				checky	=	True
		elif right < rright:
			if right >= rleft:
				checky	=	True
		
		if checky:
			if top >= rtop:
				if top < rbottom:
					return True
			elif bottom < rbottom:
				if bottom >= rtop:
					return True
			
		return False

	# -------------------------------------------------------------------
	def __str__(self):
		return '(%s, %s, %s, %s)' % (self.x, 
			self.y, 
			self.x + self.width, 
			self.y + self.height)
			
	# -------------------------------------------------------------------
	@classmethod
	def LeftEdge(self, y, rts):
		edge	=	0xffffff
		for r in rts:
			if r.y <= y and y <= r.y + r.height:
				if r.x < edge:
					edge = r.x
		
		return edge
